using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace TenantManagementService.Tenants
{
    /// <summary>
    /// 租户仓储接口
    /// </summary>
    public interface ITenantRepository : IRepository<Tenant, Guid>
    {
        /// <summary>
        /// 根据编码获取租户
        /// </summary>
        Task<Tenant?> FindByCodeAsync(string code, bool includeDetails = true, CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据编码获取租户（如果不存在则抛出异常）
        /// </summary>
        Task<Tenant> GetByCodeAsync(string code, bool includeDetails = true, CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据主机获取租户
        /// </summary>
        Task<Tenant?> FindByHostAsync(string host, bool includeDetails = true, CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据状态获取租户列表
        /// </summary>
        Task<List<Tenant>> GetListByStatusAsync(
            TenantStatus status,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据类型获取租户列表
        /// </summary>
        Task<List<Tenant>> GetListByTypeAsync(
            TenantType type,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取活跃租户列表
        /// </summary>
        Task<List<Tenant>> GetActiveTenantsAsync(
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取需要续期的租户列表
        /// </summary>
        Task<List<Tenant>> GetTenantsNeedingRenewalAsync(
            int daysThreshold = 7,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取过期的租户列表
        /// </summary>
        Task<List<Tenant>> GetExpiredTenantsAsync(
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取试用租户列表
        /// </summary>
        Task<List<Tenant>> GetTrialTenantsAsync(
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据管理员邮箱获取租户
        /// </summary>
        Task<Tenant?> FindByAdminEmailAsync(
            string adminEmail,
            bool includeDetails = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取租户统计信息
        /// </summary>
        Task<TenantStatistics> GetStatisticsAsync(CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查编码是否已存在
        /// </summary>
        Task<bool> IsCodeExistsAsync(
            string code,
            Guid? excludeTenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查主机是否已存在
        /// </summary>
        Task<bool> IsHostExistsAsync(
            string host,
            Guid? excludeTenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取租户代码列表
        /// </summary>
        Task<List<string>> GetCodesAsync(
            bool? isEnabled = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量更新租户状态
        /// </summary>
        Task<int> UpdateStatusAsync(
            List<Guid> tenantIds,
            TenantStatus status,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量禁用租户
        /// </summary>
        Task<int> DisableTenantsAsync(
            List<Guid> tenantIds,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量启用租户
        /// </summary>
        Task<int> EnableTenantsAsync(
            List<Guid> tenantIds,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取用户数量超限的租户
        /// </summary>
        Task<List<Tenant>> GetTenantsWithUserLimitExceededAsync(
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取存储超限的租户
        /// </summary>
        Task<List<Tenant>> GetTenantsWithStorageLimitExceededAsync(
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取最近创建的租户
        /// </summary>
        Task<List<Tenant>> GetRecentlyCreatedTenantsAsync(
            TimeSpan timeRange,
            int? maxCount = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取长时间未访问的租户
        /// </summary>
        Task<List<Tenant>> GetInactiveTenantsAsync(
            TimeSpan inactiveThreshold,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 搜索租户
        /// </summary>
        Task<List<Tenant>> SearchAsync(
            string keyword,
            TenantType? type = null,
            TenantStatus? status = null,
            int? maxCount = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取租户使用率统计
        /// </summary>
        Task<TenantUsageStatistics> GetUsageStatisticsAsync(
            Guid tenantId,
            CancellationToken cancellationToken = default);
    }

    /// <summary>
    /// 租户统计信息
    /// </summary>
    public class TenantStatistics
    {
        /// <summary>
        /// 总租户数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 活跃租户数
        /// </summary>
        public int ActiveCount { get; set; }

        /// <summary>
        /// 暂停租户数
        /// </summary>
        public int SuspendedCount { get; set; }

        /// <summary>
        /// 禁用租户数
        /// </summary>
        public int DisabledCount { get; set; }

        /// <summary>
        /// 试用租户数
        /// </summary>
        public int TrialCount { get; set; }

        /// <summary>
        /// 过期租户数
        /// </summary>
        public int ExpiredCount { get; set; }

        /// <summary>
        /// 按类型分组的租户数
        /// </summary>
        public Dictionary<TenantType, int> TypeDistribution { get; set; } = new();

        /// <summary>
        /// 按状态分组的租户数
        /// </summary>
        public Dictionary<TenantStatus, int> StatusDistribution { get; set; } = new();

        /// <summary>
        /// 本月新增租户数
        /// </summary>
        public int NewThisMonth { get; set; }

        /// <summary>
        /// 本月激活租户数
        /// </summary>
        public int ActivatedThisMonth { get; set; }

        /// <summary>
        /// 总用户数
        /// </summary>
        public int TotalUsers { get; set; }

        /// <summary>
        /// 总存储使用量（MB）
        /// </summary>
        public long TotalStorageUsage { get; set; }
    }

    /// <summary>
    /// 租户使用率统计
    /// </summary>
    public class TenantUsageStatistics
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户名称
        /// </summary>
        public string TenantName { get; set; } = string.Empty;

        /// <summary>
        /// 当前用户数
        /// </summary>
        public int CurrentUserCount { get; set; }

        /// <summary>
        /// 最大用户数
        /// </summary>
        public int MaxUserCount { get; set; }

        /// <summary>
        /// 用户使用率（百分比）
        /// </summary>
        public double UserUsagePercentage { get; set; }

        /// <summary>
        /// 当前存储使用量（MB）
        /// </summary>
        public long CurrentStorageUsage { get; set; }

        /// <summary>
        /// 最大存储空间（MB）
        /// </summary>
        public long MaxStorageSize { get; set; }

        /// <summary>
        /// 存储使用率（百分比）
        /// </summary>
        public double StorageUsagePercentage { get; set; }

        /// <summary>
        /// 是否用户数超限
        /// </summary>
        public bool IsUserLimitExceeded { get; set; }

        /// <summary>
        /// 是否存储超限
        /// </summary>
        public bool IsStorageLimitExceeded { get; set; }

        /// <summary>
        /// 订阅剩余天数
        /// </summary>
        public int? SubscriptionDaysRemaining { get; set; }

        /// <summary>
        /// 是否即将过期
        /// </summary>
        public bool IsExpiringSoon { get; set; }

        /// <summary>
        /// 最后访问时间
        /// </summary>
        public DateTime? LastAccessTime { get; set; }

        /// <summary>
        /// 最后访问描述
        /// </summary>
        public string LastAccessDescription { get; set; } = string.Empty;
    }
}